 //删除的方法

   function delCurr(id){
	 
	  $.ajax({
            url:"delcurrioulum",
            type:"get",
            data:{
            	id:id
            },
            
            success:function(data){
                if(data=="1"){
                   alert("删除成功");
                   $('#brscjl').datagrid('reload');
                   $('#scjl').datagrid('reload');  
                }else{
                	alert("删除失败");
                }
            }
        });
   };
	   
   //还原的方法
   function huanYuan(id){
	   
	   $.ajax({
            url:"huancurr",
            type:"get",
            data:{
            	id:id
            },
            success:function(data){
                if(data=="1"){
                   alert("还原成功")
                   $('#brscjl').datagrid('reload'); 
                   
                }else{
                	alert("还原失败");
                }
            }
        });
   };
   var list=new Array();
$(function(){
	
	
		
	   //学习园地   教师模块选项卡
	   $('#jiaoshi').tabs({    
		    border:1,
		    justified:true,
		    closable:true,
		}); 
	  
	   //下拉框
	   $('#ff').combotree({    
		    url: 'tree.do',    
		    required: true ,//必填
		    editable:false,//不可编辑	
			    	
		    loadFilter: function(rows){
		    	
				return convert(rows);
			},
			/*onLoadSuccess: function () {  //加载完成后,设置选中第一项
	          var val = $(this).combotree('getData');
	          for(var item in val[0]) {
	               $(this).combotree('select', val[0][item]);
	          }
	          $(this).combotree('select',val[0].specialty_name);
	        }*/
		});
	   //处理返回数据
	   function convert(rows){
			function exists(rows, parentId){
				for(var i=0; i<rows.length; i++){
					if (rows[i].specialty_id == parentId) 
						return true;
				}
				return false;
			}
			
			var nodes = [];
			// 获取顶级节点
			for(var i=0; i<rows.length; i++){
				var row = rows[i];
				if (!exists(rows, row.parentId)){
					nodes.push({
						id:row.specialty_id,
						text:row.specialty_name,
						
					});
				}
			}
			
			
			var toDo = [];
			for(var i=0; i<nodes.length; i++){
				toDo.push(nodes[i]);
			}
			while(toDo.length){
				var node = toDo.shift();	// 父節點
				
				//獲取子節點 
				for(var i=0; i<rows.length; i++){
					var row = rows[i];
					if (row.parentId == node.id){
						
						var child = {id:row.specialty_id,text:row.specialty_name,state:"open"};
						
						if (node.children){
							node.children.push(child);
						} else {
							node.children = [child];
						}
						toDo.push(child);
					}
				}
			}
			return nodes;
		};
		var jj;
	   //下拉框的确定按钮
	   $('#btn').bind('click', function(){
		   
		   if ($("#ff").combotree('getValue')) {
			   $("#sc").attr("style","display:block;margin-top: 100px;margin-left: 300px;");//显示
			   $("#kuang").attr("style","display:none;");//隐藏下拉框的div   
			   
			   //获取下拉框的值
			   var gh=$("#ff").combotree('getValue');
			   console.log(gh);
			   jj=gh;
			   
		   }
	    });
	   
	   
	   
	   //上传的方法
	   $('#jiang').bind('click', function(){
		   var wen=$("input[name='description']").val();//命名的文件名称
	
		   var kongjian=$("input[name='kongjian']:checked").val();//是否私人空间
		   
		   
		 
	        
	        var form = new FormData(document.getElementById("tf"));
	        $.ajax({
	            url:"upload?jj="+jj+"&wen="+wen+"&kongjian="+kongjian,
	            type:"post",
	            data:form,
	            processData:false,
	            contentType:false,
	            success:function(data){
	                if(data=="1"){
	                	
	                    alert("上传成功")
	                    window.location.reload();
	                }else if(data=="3"){
	                    alert("类型错误")
	                }else{
	                	alert("上传失败");
	                }
	            }
	        });
	    });
	   
	   //绑定返回的方法
	   $('#fan').bind('click', function(){
		   $("#sc").attr("style","display:none;margin-top: 100px;margin-left: 300px;");
		   $("#kuang").attr("style","display:block;margin-top: 50px;margin-left: 400px;"); 
	   });
	 //验证文件名的输入
		/*$("input[name='description']").validatebox({
			required:true,
			validType:'length[1,20]',
			missingMessage:'请输入文件名',
			invalidMessage:'文件名在1-20位',
		});*/
	   
	//全部教师上传记录的搜索框
	   var sou="";
	    $('#sousuo').bind('click', function(){
			sou=$("#sou").val();
			
			
		
			 var op = $("#scjl").datagrid("options");//获取 option设置对象
             op.url = "getListcurrdatagrid?sou=" +$("#sou").val() ;//设置url
			$('#scjl').datagrid('reload');
	    });
	
	//全部教师上传记录
	   $('#scjl').datagrid({    
		    url:'getListcurrdatagrid?sou='+sou,
		    singleSelect:true,
		    rownumbers:true,
			pagination:true,
		    columns:[[    
				{
					field:"user",
					title:"上传老师",
					 align: 'center',
					width:200,
					
					formatter:function(value,row,index){
						
						var bb = value.user_name;
						return bb;
					}
				},
				{
					field:"curriculum_createtime",
					title:"上传时间",
					 align: 'center',
					width:300,
					formatter : crtTimeFtt
				},
				{
					field:"upload_state",
					title:"文件类型",
					 align: 'center',
					width:150,
				},
				{
					field:"curriculum_name",
					title:"教程名称",
					 align: 'center',
					width:300,
				},	
				{
					field:"curriculum_id",
					title:"操作",
					align: 'center',
					width:120,
					 formatter: function (val, row, index) {
			             var btn = "<a href='download?filename="+row.curriculum_name+"&filetype="+row.upload_state+"' >下载</a>";
			             return btn;
			         }
				}
		    ]],   
		});
	 
	  
	  //上传记录的点击事件
	  $('#hehe').bind('click', function(){
		 
		 // $("#qb").attr("style","display:none;");
		 
		  
		  //$("#gr").attr("style","display:block;");
		  
		  
	  });
	  $('#juju').bind('click', function(){
		 
		  //$("#qb").attr("style","display:block;");
		  
		  
		  //$("#gr").attr("style","display:none;");
		  
		  
	  });
	  
	  //教师个人查询
	  var grsou="";
	    $('#grsousuo').bind('click', function(){
			grsou=$("#grsou").val();
			 var op = $("#brscjl").datagrid("options");//获取 option设置对象
           op.url = "getsinglelistcurr?grsou=" +$("#grsou").val() ;//设置url
			$('#brscjl').datagrid('reload');
	    });
	  
	  //教师个人上传记录
	   $('#brscjl').datagrid({    
		    url:'getsinglelistcurr?grsou='+grsou,
		    singleSelect:true,
		    rownumbers:true,
			pagination:true,
		    columns:[[    
				{
					field:"user",
					title:"上传老师",
					 align: 'center',
					width:200,
					
					formatter:function(value,row,index){
						
						var bb = value.user_name;
						return bb;
					}
				},
				{
					field:"curriculum_createtime",
					title:"上传时间",
					 align: 'center',
					width:300,
					formatter : crtTimeFtt
				},
				{
					field:"upload_state",
					title:"文件类型",
					 align: 'center',
					width:150,
				},
				{
					field:"curriculum_name",
					title:"教程名称",
					 align: 'center',
					width:300,
				},
				{
					field:"curriculum_del",
					title:"状态",
					 align: 'center',
					width:100,
					formatter:function(value,row,index){
						
						if (row.curriculum_del==0) {
							
							var gg='未删除';
							return gg;
						}
						var xx='已删除';
						return xx;
					}
				},	
				{
					field:"curriculum_id",
					title:"操作",
					align: 'center',
					width:120,
					 formatter: function (val, row, index) {
						 var btn;
						 if (row.curriculum_del==0) {
				             btn = '<a  onclick="delCurr(\'' + row.curriculum_id + '\')"  href="javascript:void(0)">删除</a>';
				             return btn;
						}
				          btn = '<a  onclick="huanYuan(\'' + row.curriculum_id + '\')"  href="javascript:void(0)">还原</a>';
			             return btn;
			         }
				}
		    ]],   
		});
	  //时间格式化的方法
	  function crtTimeFtt(val, row) {
			 if (val != null) {
			   var date = new Date(val);
			   return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+'         '+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
			  }
		};
		
	//时间框
	$('#dt').datetimebox({    
	    required: true,    
	    showSeconds: false,   
	});


	//跳出来选题的面板
	
	$('#p').panel({ 
		  width:580,    
		  height:250,
		  closed:true,
		  closable : true,//关闭的图标
		  title: '选择练习题',    
		});
	//保存题目的面板
	$('#g').panel({ 
		  width:580,    
		  height:250,
		  closed:true,
		  closable : true,//关闭的图标
		  title: '作业练习题', 
		  tools: [{    
			    iconCls:'icon-remove',    
			    handler:function(){
			    	var selRows = $('#cunti').datagrid('getChecked'); 
			    	console.log(selRows);
			    	console.log(selRows[0].subjectId);
			    	$.ajax({
			            url:"delcunti",
			            type:"get",
			            data:{
			            	id:selRows[0].subjectId
			            },
			            success:function(data){
			                if(data=="1"){
			                    alert("删除成功")
			                     var op = $("#cunti").datagrid("options");//获取 option设置对象
			                    	op.url = "cunti" ;
			                    $("#cunti").datagrid('reload');
			                    
			                }else{
			                	alert("删除失败");
			                }
			            }
			        });
			    }    
			  }]    

		});
	//保存题目的表格
	  $('#cunti').datagrid({    
		    url:'cunti',
		    singleSelect:true,
		    rownumbers:true,
		    columns:[[    
				{
					field:"subjectId",
					title:"编号",
					 align: 'center',
					 checkbox:true,
					width:30,
				},
				{
					field:"subjectName",
					title:"题目",
					 align: 'center',
					width:150,
					
				},
				{
					field:"subjectType",
					title:"题目类型",
					 align: 'center',
					width:80,
					formatter:function(value,row,index){
						
						if (row.subjectType==1) {
							var dan="单选";
							return dan;
						}
						
						var bb = "多选";
						return bb;
					}
				},
				{
					field:"optionA",
					title:"选项A",
					 align: 'center',
					width:80,
				},
				{
					field:"optionB",
					title:"选项B",
					 align: 'center',
					width:80,
				},	
				{
					field:"optionC",
					title:"选项C",
					align: 'center',
					width:80,
				},
				{
					field:"optionD",
					title:"选项D",
					align: 'center',
					width:80,
				}
		    ]], 
		 
		});
	
	//题目的表格
	   $('#xuanti').datagrid({    
		    url:'',
		    singleSelect:true,
		    rownumbers:true,
			pagination:true,
		    columns:[[    
				{
					field:"subjectId",
					title:"编号",
					 align: 'center',
					 checkbox:true,
					width:30,
				},
				{
					field:"subjectName",
					title:"题目",
					 align: 'center',
					width:150,
					
				},
				{
					field:"subjectType",
					title:"题目类型",
					 align: 'center',
					width:80,
					formatter:function(value,row,index){
						
						if (row.subjectType==1) {
							var dan="单选";
							return dan;
						}
						
						var bb = "多选";
						return bb;
					}
				},
				{
					field:"optionA",
					title:"选项A",
					 align: 'center',
					width:80,
				},
				{
					field:"optionB",
					title:"选项B",
					 align: 'center',
					width:80,
				},	
				{
					field:"optionC",
					title:"选项C",
					align: 'center',
					width:80,
				},
				{
					field:"optionD",
					title:"选项D",
					align: 'center',
					width:80,
				}
		    ]], 
		    onLoadSuccess:function(data){
		    		if (data.total==0) {
		    			$.messager.alert('你好','没有该课程的题目');
					}  
		    }
		});
	   $('#xuanti').datagrid({
		   //当勾选的时候
		   
		   onCheck:function(index, row){
			   var op = $("#cunti").datagrid("options");//获取 option设置对象
				op.url="cunti?id="+row.subjectId;
				
				//list.unshift() ;
				
				console.log(row);
				
				$("#cunti").datagrid('reload');
				
			   $("#g").panel("open");
			   setTimeout(function (){
				   
				   $("#xuanti").datagrid('clearSelections');
				}, 1000);
				 
		   }
	   })
	//关闭选题的操作面板
	   $("#guanti").bind("click",function(){
		   $("#p").panel("close");
		   $("#g").panel("close");
	   });
	   $("#kaiti").bind("click",function(){
		   $("#p").panel("open");
		   $("#g").panel("open");
	   })
	//选择学生的面板
	/*$('#xstu').panel({    
		  width:500,    
		  height:150,
		  closed:true,
		  closable : true,//关闭的图标
		  title: 'My Panel',    
		  
		});*/
	//指定班级的下拉框
	$('#clas').combobox({    
	    url:'clazzlist',    
	    valueField:'clazz_id',    
	    textField:'clazz_name',
	    onLoadSuccess: function () {  //加载完成后,设置选中第一项
	          var val = $(this).combobox('getData');
	          for(var item in val[0]) {
	               $(this).combobox('select', val[0][item]);
	          }
	          $(this).combobox('select',val[0].clazz_id);
	    }
	}); 
	
	//布置作业的按钮
	   
    /*$('#tj').bind('click', function(){
    	
    	var fs=$("input[name='wo']:checked").val()
    	if (fs==0) {
    		var ss=$('#clas').combobox("getValue");
		}else{
			
		}
    	
    	
    	$.ajax({
            url:"shengword",
            type:"get",
            data:{
            	wordName:$("#work").val(),
            	//workdate:$('#dt').datebox('getValue'),
            	//zhangjie:$("#ff1").combotree('getValue'),
            },
            success:function(data){
                if(data=="1"){
                	 window.location.reload();
                	$.messager.alert('你好','布置成功');
                }else{
                	$.messager.alert('你好','作业布置失败');
                }
            }
        });
    }); */
    //指定班级时的点击事件
    $("#clazz").bind('click',function(){
    	$("#cla").attr("style","display:block;");//班级下拉框出现
    	//$("#xstu").panel("close");//学生面板关闭
    });
//    $("#stu").bind('click',function(){
//    	$("#cla").attr("style","display:none;");//班级下拉框隐藏
//    	$("#xstu").panel("open");//学生面板打开
//    });
    //下拉框
   $('#ff1').combotree({    
	    url: 'tree.do',    
	    required: true ,//必填
	    editable:false,//不可编辑	
		    	
	    loadFilter: function(rows){
	    	
			return convert(rows);
		},
		/*onLoadSuccess: function () {  //加载完成后,设置选中第一项
          var val = $(this).combotree('getData');
          for(var item in val[0]) {
               $(this).combotree('select', val[0][item]);
          }
          $(this).combotree('select',val[0].specialty_name);
        }*/
	});
   //打开选试题面板
   
   $("#zuoye").bind("click",function(){
	 
		
	   var sa=$("#ff1").combotree('getValue');
	   	
		var op = $("#xuanti").datagrid("options");//获取 option设置对象
		op.url="xuanti?id="+sa;
	
		$("#xuanti").datagrid('reload');
		$("#p").panel("open");
		
   });
   
	 
});
